﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace ASoft.Service
{
    public class TcpService
    {

        public void Open()
        {
        }

        public void Close()
        {
        }

        public void Send(byte[] data)
        {
            var sendClient = new TcpClient();
            sendClient.Connect(IPAddress.Parse(IP), Port);
            var stream = sendClient.GetStream();
            stream.Write(data, 0, data.Length);
            stream.Flush();
            sendClient.Close();
        }

        public byte[] Receive()
        {
            byte[] data = new byte[256];
            var receiveClient = new TcpClient(IP, Port);
            var stream = receiveClient.GetStream();
            int length = stream.Read(data, 0, data.Length);
            receiveClient.Close();
            return data.Take(length).ToArray();
        }

        public string IP { get; set; }

        public int Port { get; set; }
    }
}
